﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBO;
using CommonLibrary;
using DBO.Economy;
using ClientBusinessManagement;
using ClientUtility;
using System.Runtime.Serialization;

namespace CustomControls.Complex
{
    public partial class TroopControl : UserControl
    {
        public TroopControl()
        {
            InitializeComponent();
            counterControl1.onMinus += new EventHandler(counterControl1_onMinus);
            counterControl1.onPlus += new EventHandler(counterControl1_onPlus);
            this.pictureBox1.Click += new EventHandler(pictureBox1_Click);
            counterControl1.Enabled = true;
        }

        public void ToggleSelected(bool selected)
        {
            if (selected)
                pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            else
                pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

        public event EventHandler onBuy;
        public event EventHandler onSell;

        void pictureBox1_Click(object sender, EventArgs e)
        {
            if (TroopNumber > 0)
                onClick.Invoke(this, e);
        }

        public event EventHandler onClick;

        public bool IsNegative()
        {
            return counterControl1.CurrentValue < 0;
        }

        void counterControl1_onPlus(object sender, EventArgs e)
        {
            onBuy.Invoke(this, null);
        }

        void counterControl1_onMinus(object sender, EventArgs e)
        {
            onSell.Invoke(this, null);
        }

        public int TroopNumber
        {
            get { return counterControl1.CurrentValue; }
            set
            {
                counterControl1.CurrentValue = value;
                if (value <= 0)
                    counterControl1.BackColor = System.Drawing.Color.Red;
                else
                    counterControl1.BackColor = System.Drawing.Color.Gray;
            }
        }

        public bool CanAdd
        {
            get { return counterControl1.CanAdd; }
            set
            {
                counterControl1.CanAdd = value;
                /*if (value)
                    InfoLabel.Text = "";
                else
                    InfoLabel.Text = "Time left : fixme";*/
            }
        }

        public Image Image
        {
            get { return this.pictureBox1.Image; }
        }

        public string Troop
        {
            get
            {
                return _troop;
            }
            set
            {
                pointsLabel.Text = ArmyConstants.GetPointPrice(value) + " pts";
                _troop = value;
                statisticsControl1.Statistics = ClientDataSingleton.Instance.ResourceRepository.Get<UnitStatistics>(value);
                resourcesControl1.Resources = ClientDataSingleton.Instance.ResourceRepository.Get<Resources>(value);
                NameLabel.Text = value;
                pictureBox1.Image = getTroopImage(value.ToLower());
                if (value.ToLower() == "footman" || value.ToLower() == "archer" || value.ToLower() == "knight")
                    RaceLabel.Text = "Heirs";
                else
                    RaceLabel.Text = "Renegade";
                TroopNumber = ClientDataHandler.CountSlots(value.ToLower());
            }
        }
        private string _troop;

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private Image getTroopImage(string type)
        {
            Image e = null;

            switch (type.ToLower())
            {
                case "footman":
                    e = Properties.Resources.swordman_stand;
                    break;

                case "archer":
                    e = Properties.Resources.archer_stand;
                    break;

                case "knight":
                    e = Properties.Resources.knight_stand;
                    break;

                case "militian":
                    e = Properties.Resources.militian_stand;
                    break;

                case "knife_thrower":
                    e = Properties.Resources.rogue_stand;
                    break;

                case "ranger":
                    e = Properties.Resources.ranger_stand;
                    break;

                default:
                    throw new ClientException("Unknown entity : " + type + ".");
            }

            return e;
        }

        private void counterControl1_Load(object sender, EventArgs e)
        {

        }

    }
}
